package com.icloud.admin.query;

import com.github.pagehelper.PageInfo;
import com.icloud.admin.query.dto.RoleDTO;
import com.icloud.admin.query.mapper.MenuDTOMapper;
import com.icloud.admin.query.mapper.ResourceDTOMapper;
import com.icloud.admin.query.mapper.RoleDTOMapper;
import com.icloud.admin.query.dto.MenuDTO;
import com.icloud.admin.query.dto.ResourceDTO;
import com.icloud.framework.query.TreeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AccessQueryService {


    @Autowired
    private RoleDTOMapper roleDTOMapper;

    @Autowired
    private MenuDTOMapper menuDTOMapper;

    @Autowired
    private ResourceDTOMapper resourceDTOMapper;


    public PageInfo<RoleDTO> findRolePaging(int pageNum, int pageSize) {
        return roleDTOMapper.findPaging(pageNum, pageSize).toPageInfo();
    }

    public List<MenuDTO> findeMenusByRoleId(String roleId) {
        return TreeUtils.createTree(menuDTOMapper.findByRoleId(roleId));
    }

    public List<MenuDTO> findMenusAssignedByRoleId(String roleId) {
        return TreeUtils.createTree(menuDTOMapper.findAssignedByRoleId(roleId));
    }


    public List<MenuDTO> findMenuTree() {
        return TreeUtils.createTree(menuDTOMapper.findAll());
    }

    public MenuDTO findMenu(String id) {
        return menuDTOMapper.findById(id);
    }


    /**
     * 查询角色所拥有的资源
     *
     * @param roleId
     * @return
     */
    public List<ResourceDTO> findResourceByRoleId(String roleId) {
        return resourceDTOMapper.findByRoleId(roleId);
    }

    /**
     * 查询菜单下挂载的资源
     *
     * @param menuId
     * @return
     */
    public List<ResourceDTO> findResourceByMenuId(String menuId) {
        return resourceDTOMapper.findByMenuId(menuId);
    }

    public List<ResourceDTO> findResourceAll() {
        return resourceDTOMapper.findAll();
    }

    public List<MenuDTO> findMenusByUserId(String userId){
        return  menuDTOMapper.findByUserId(userId);
    }


}
